#!/bin/bash
export OSEXEC="sudo -u gaussdb -i "
export DBSET="$OSEXEC gs_guc set -D /opt/gaussdb/data"

if [ -s "/opt/gaussdb/bin/gaussdb" ]; then
                BINARY_ALREADY_EXISTS='true'
else 
chmod +x /opt/gaussdb/GaussDB-Kernel_506.0.0.SPC0100_Kylin_64bit.bin 
chown gaussdb: /opt/gaussdb -R 
$OSEXEC /bin/bash -c "cd /opt/gaussdb && ./GaussDB-Kernel_506.0.0.SPC0100_Kylin_64bit.bin && rm -rf GaussDB-Kernel_506.0.0.SPC0100_Kylin_64bit.bin" 
fi

if [ -s "/opt/gaussdb/data/PG_VERSION" ]; then
                DATABASE_ALREADY_EXISTS='true'
else 
$OSEXEC gs_initdb --pgdata=/opt/gaussdb/data --nodename=primary --pwpasswd=Gaussdb@123 --encoding=UTF-8 --locale=C
$DBSET -c "a_format_dev_version='s6'"
$DBSET -c "a_format_version='10c'"
$DBSET -c "ai_watchdog_oom_other_used_memory_threshold=-1"
$DBSET -c "ai_watchdog_oom_process_threshold=1"
$DBSET -c "character_set_client='UTF8'"
$DBSET -c "checkpoint_segments=1024"
$DBSET -c "client_encoding='UTF8'"
$DBSET -c "cursor_sharing=off"
$DBSET -c "dcf_rep_append_thread_num=3"
$DBSET -c "dcf_run_mode=1"
$DBSET -c "effective_cache_size=160MB"
$DBSET -c "enable_mergejoin=off"
$DBSET -c "enable_nestloop=off"
$DBSET -c "enable_slot_log=on"
$DBSET -c "enable_thread_pool=on"
$DBSET -c "explain_perf_mode='pretty'"
$DBSET -c "extra_float_digits=3"
$DBSET -c "gs_format_behavior_compat_options='sqrt_karatsuba'"
$DBSET -c "instr_unique_sql_combination_options='in_clause,forbid_select_for_update'"
$DBSET -c "instr_unique_sql_count=200000"
$DBSET -c "listen_addresses='*'"
$DBSET -c "local_bind_address='0.0.0.0'"
$DBSET -c "log_line_prefix='%m %n %u %d %h %p %S %x %a %e'"
$DBSET -c "log_min_duration_statement=100ms"
$DBSET -c "log_timezone='Asia/Shanghai'"
$DBSET -c "maintenance_work_mem=16MB"
$DBSET -c "max_compile_packages=2000"
$DBSET -c "max_concurrent_autonomous_transactions=1000"
$DBSET -c "max_connections=3000"
$DBSET -c "max_files_per_process=1024"
$DBSET -c "max_process_memory=24GB"
$DBSET -c "max_replication_slots=20"
$DBSET -c "max_wal_senders=20"
$DBSET -c "numa_distribute_mode='all'"
$DBSET -c "password_effect_time=0"
$DBSET -c "password_reuse_time=0"
$DBSET -c "product_version='V2.0'"
$DBSET -c "recovery_max_workers=4"
$DBSET -c "recovery_time_target=0"
$DBSET -c "session_timeout=0"
$DBSET -c "sql_beta_feature='a_style_coerce,sel_semi_poisson, sel_expr_instr, rand_cost_opt, page_est_opt, param_path_opt'"
$DBSET -c "ssl=off"
$DBSET -c "ssl_ca_file=cacert.pem"
$DBSET -c "standby_shared_buffers_fraction=1"
$DBSET -c "system_view_version=1"
$DBSET -c "thread_pool_attr='16,2,(nobind)'"
$DBSET -c "vacuum_cost_delay=1"
$DBSET -c "vacuum_cost_limit=1000"
$DBSET -c "verify_log_buffers=16MB"
$DBSET -c "wal_buffers=256MB"
$DBSET -c "wal_keep_segments=128"
$DBSET -c "work_mem=64MB"
$DBSET -c "xloginsert_locks=16"
$DBSET -c "password_encryption_type=1"
$DBSET -c "password_lock_time=1"
$DBSET -c "failed_login_attempts=100"
$DBSET -c "wal_level='logical'"
$DBSET -c "log_statement='ddl'"
$DBSET -c "log_error_verbosity='verbose'"
$DBSET -c "track_activity_query_size=40960"
$DBSET -c "lockwait_timeout=0"
$DBSET -c "update_lockwait_timeout=0"
$DBSET -c "idle_in_transaction_timeout=0"
$DBSET -c "behavior_compat_options='aformat_null_test,aformat_regexp_match,allow_procedure_compile_check,bind_procedure_searchpath,compat_cursor,convert_string_digit_to_numeric,correct_to_number,current_sysdate,display_leading_zero,dynamic_sql_compat,enable_bpcharlikebpchar_compare,enable_case_when_alias,enable_crosstype_integer_operator,enable_ora_joinop_in_updatestmt,enable_use_ora_timestamptz,end_month_calculate,forbid_package_function_with_prefix,forbid_skip_tableof_empty_str_elem,forbid_update_multi_same_tables,plsql_rollback_keep_user,plsql_security_definer,plstmt_implicit_savepoint,proc_implicit_for_loop_variable,proc_outparam_override,proc_outparam_transfer_length,rownum_type_compat,show_full_error_lineno,sys_function_without_brackets,tableof_elem_constraints,time_constexpr_compact,truncate_numeric_tail_zero,unbind_divide_bound,varray_compat'"
$DBSET -c "disable_keyword_options='datetime,regexp,rlike,zerofill,unit'"
$DBSET -c "max_stack_depth=4MB"
$DBSET -c "ddl_invalid_mode='invalid'"
$DBSET -c "enable_force_create_obj=on"
$DBSET -c "max_compile_functions=100000"
$DBSET -c "max_recursive_times=1500"
$DBSET -c "enable_recyclebin=on"
$DBSET -c "undo_retention_time=900"
$DBSET -c "audit_resource_policy=off"
$DBSET -c "audit_file_remain_time=180"
$DBSET -c "log_directory='/opt/gaussdb/log/pg_log/dn_6001'"
$DBSET -c "audit_directory='/opt/gaussdb/log/pg_audit/dn_6001'"
$DBSET -c "asp_log_directory='/opt/gaussdb/log/asp_data/dn_6001'"
$DBSET -c "perf_directory='/opt/gaussdb/log/pg_perf/dn_6001'"
$DBSET -c "query_log_directory='/opt/gaussdb/log/sql_monitor/dn_6001'"
$DBSET -h "host     all      all  0.0.0.0/0    md5"
$OSEXEC gs_ctl reload -D /opt/gaussdb/data
$OSEXEC gs_ctl start -D /opt/gaussdb/data
$OSEXEC gsql -d postgres -c "begin create user admin password 'Gaussdb@123' sysadmin monadmin; exception when others then null; end;"
$OSEXEC gs_ctl stop -D /opt/gaussdb/data
fi

$OSEXEC gaussdb -D /opt/gaussdb/data